import $service from '../../../../service.js';
var util = require('../../../../utils/util.js')
var that;
var page = 1;
var queryzt='';
const app = getApp();
Page({
    data: {
        inquiryList:[],
        tab:1,
        startdate:'',
        enddate:'',
        starttime:'2000-01-01',
        endtime:'',
        isshow:false,
        loadingText: '',
        loadingType: true,
        noMore: false,
        diagnosisList:[]    //问诊诊断列表
    },
    onLoad(e) {

    },
    onShow() {
        page = 1
        that = this.data;
        this.getInterrogationHistoryList();
    },
    getInterrogationHistoryList() {
        let params = {
            "current": page,
            "endTime": that.enddate,
            "size": 10,
            "startTime": that.startdate
          }
        app.http.getInterrogationHistoryList(params).then(res => {
            let list = $service.deepClone(res.data.list);
            if (this.data.noMore) return
            setTimeout(()=>{
                if (that.inquiryList.length == res.data.total) {
                    this.setData({
                        noMore: true
                    })
                }
            },500)
            if (page != 1) {
                list = that.inquiryList.concat(list)
            }
            let diagnosisList = [];
            list.forEach((item)=>{
                switch(Number(item.inquiryType)){
                    case 1:
                        item.inquiryType = "视频问诊"
                        break;
                    case 2:
                        item.inquiryType = "图文问诊"
                        break;
                    case 3:
                        item.inquiryType = "家庭问诊"
                        break;
                    case 4:
                        item.inquiryType = "复诊问诊"
                        break;
                }
                diagnosisList.push(JSON.parse(item.diagnosis))
            });

            this.setData({
                inquiryList: list,
                diagnosisList
            });
        }).catch(err => {
            $service.showToast(err.msg)
        }).finally(() => {
            wx.stopPullDownRefresh()
        })
    },
    onPullDownRefresh() {
        page = 1;
        this.setData({
            noMore: false
        })
        this.getInterrogationHistoryList();
    },
    onReachBottom: function() {
        if (!that.noMore){
            page ++
            this.getInterrogationHistoryList();
        }
    },
    bookTime(e) {
        page = 1;
        let time = e.detail.time;
        let type = e.currentTarget.dataset.type;
        if (type === 'start') {
            if (that.enddate) {
                if ($service.timeFormat(time) > $service.timeFormat(that.enddate)) {
                    $service.showToast('结束时间不可小于开始时间');
                    return
                }
                that.startdate =  time;
            } else {
                that.startdate =  time;
            }
        } else {
            if (that.startdate) {
                if ($service.timeFormat(that.startdate) > $service.timeFormat(time)) {
                    $service.showToast('结束时间不可小于开始时间');
                    return
                }
            }
            that.enddate =  time;
        }
        this.setData({
            noMore: false,
            startdate: that.startdate,
            enddate: that.enddate
        })
        this.getInterrogationHistoryList();
    },
    // gotopage(e){
    //     var index = e.currentTarget.dataset.index;
    //     wx.navigateTo({
    //         url : 'personal_interrogation_detalis/personal_interrogation_detalis?id=' + this.data.inquiryList[index].id
    //     })
    // },
    //跳转到问诊详情页(清单页面)
    gotopage(e){
        let index = e.currentTarget.dataset.index
        $service.openPage('/pages/user_side/my_doctor/inventory/inventory?id='+this.data.inquiryList[index].id)
    },
    //跳转到对应的医生主页面
    goAgain(e){
        let index = e.currentTarget.dataset.index
        $service.openPage('/pages/user_side/index/index_doctorlist/index_doctor_details/index_doctor_details?doctorId='+this.data.inquiryList[index].doctorId)
    }
})
